<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="author" content="lijinbo" />
    <meta name="cdnload" content="jquery" />
    <title>6-02轮播图</title>
  </head>
  <style>
    .slider_content_box {
      position: relative;
    }

    .slider_content_box,
    .slider_item_box {
      width: 300px;
      height: 160px;
      overflow: hidden;
      position: absolute;
    }

    .slider_item_box img {
      max-width: 100%;
      max-height: 100%;
    }
  </style>

  <body>
    <div class="slider_max">
      <p>
        <button id="prev">prev</button>
        <button id="next">next</button>
      </p>
      <div class="slider_content_box"></div>
    </div>
    <script type="module">
      import { startAnimation } from '/utils/module/McAnimation.js'
      // 创建图片
      const imgList = [
        'https://img2.ali213.net/picfile/News/2021/03/17/584_2021031754445704.jpg',
        'https://img2.ali213.net/picfile/News/2021/03/17/584_2021031754444310.jpg',
        'https://img2.ali213.net/picfile/News/2021/03/17/584_2021031754445973.jpg'
      ]
      for (let i = 0; i < imgList.length; i++) {
        let imgDom = $(`<div class="slider_item_box"><img src="${imgList[i]}"></div>`)
        if (i) {
          imgDom.css('left', '300px')
        }
        $('.slider_content_box').append(imgDom)
      }
      let allBox = $('.slider_item_box')
      let acitveIndex = 0

      // 点击切换事件
      $('#prev').on('click', function () {
        startAnimation(allBox[acitveIndex], { left: 300 })
        acitveIndex--
        if (acitveIndex < 0) {
          acitveIndex = allBox.length - 1
        }
        allBox.eq(acitveIndex).css('left', '-300px')
        startAnimation(allBox[acitveIndex], { left: 0 })
      })
      $('#next').on('click', function () {
        startAnimation(allBox[acitveIndex], { left: -300 })
        acitveIndex++
        if (acitveIndex >= allBox.length) {
          acitveIndex = 0
        }
        allBox.eq(acitveIndex).css('left', '300px')
        startAnimation(allBox[acitveIndex], { left: 0 })
      })
    </script>
  </body>
</html>
